<?php
namespace app\index\controller;
use think\Controller;
use my\CCPRestSDK;
use my\GeetestLib;
class Login extends Controller{

    public function index(){

        return view();
    }

    public function register(){


        return view();
    }

    //滑块验证1
    public function start(){
        $id_test="d75d3fb73ff7b9e262e96c37a51060bf";
        $key_test="0c4fada828d154ecc6153258ba699fe3";
        $GtSdk = new GeetestLib( $id_test, $key_test);
        session_start();

        $data = array(
            "user_id" => "test", # 网站用户id
            "client_type" => "web", #web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
            "ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP
        );

        $status = $GtSdk->pre_process($data, 1);
        $_SESSION['gtserver'] = $status;
        $_SESSION['user_id'] = $data['user_id'];
        echo $GtSdk->get_response_str();
    }

    //滑块验证2
    public function verify(){
        $id_test="d75d3fb73ff7b9e262e96c37a51060bf";
        $key_test="0c4fada828d154ecc6153258ba699fe3";
        session_start();
        $GtSdk = new GeetestLib($id_test, $key_test);
        $data = array(
            "user_id" => $_SESSION['user_id'], # 网站用户id
            "client_type" => "web", #web:电脑上的浏览器；h5:手机上的浏览器，包括移动应用内完全内置的web_view；native：通过原生SDK植入APP应用的方式
            "ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP
        );


        if ($_SESSION['gtserver'] == 1) {   //服务器正常
            $result = $GtSdk->success_validate($_POST['geetest_challenge'], $_POST['geetest_validate'], $_POST['geetest_seccode'], $data);
            if ($result) {
                echo '{"status":"success"}';
            } else{
                echo '{"status":"fail"}';
            }
        }else{  //服务器宕机,走failback模式
            if ($GtSdk->fail_validate($_POST['geetest_challenge'],$_POST['geetest_validate'],$_POST['geetest_seccode'])) {
                echo '{"status":"success"}';
            }else{
                echo '{"status":"fail"}';
            }
        }
    }



}


